//
//  GHTabbar.m
//  12-guhu_weibo
//
//  Created by GHapple on 15-1-31.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "GHTabbar.h"
#import "GHtabbarBtn.h"

@interface GHTabbar ()

@property(nonatomic,strong)NSMutableArray *itemArray;
@property(nonatomic,strong)GHtabbarBtn* btn;
@property(nonatomic,weak)UIButton *plusBtn;

@end


@implementation GHTabbar

- (NSMutableArray *)itemArray{

    if (_itemArray == nil) {
        _itemArray = [NSMutableArray array];
    }
    return  _itemArray;
}
// 添加加号按钮
-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder ]) {
        [self setplusBtn];
    }
    return  self;
}

- (instancetype)initWithFrame:(CGRect)frame{

    if (self  = [super initWithFrame:frame]) {
        [self setplusBtn];
    }
    return  self;
}
- (void)setplusBtn{
    self.backgroundColor = [UIColor whiteColor];
    // 创建按钮
    UIButton *plusBtn = [[UIButton alloc]init];
    // 设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [self addSubview:plusBtn];
    self.plusBtn = plusBtn;
}




- (void)additem:(UITabBarItem*)item{
    GHtabbarBtn *btn = [[GHtabbarBtn alloc]init];
   
    btn.item = item;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    btn.tag = self.itemArray.count;
    [self.itemArray addObject:btn];

    // 为按钮绑定tag
    
    if (self.itemArray.count == 1) {
        [self btnClick:btn];
    }
    
}


// 按钮的点击
- (void)btnClick:(GHtabbarBtn*)btn{
    
   
    
    self.btn.selected  = NO;
    btn.selected = YES;
    self.btn = btn;
    
    // 实现按钮的动画效果
   [UIView animateWithDuration:1.0 animations:^{
    btn.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.1 animations:^{
         btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        btn.transform = CGAffineTransformIdentity;
    }];
}];
//    NSLog(@"%ld-------%ld",self.btn.tag,btn.tag);
    if ([self.delegate respondsToSelector:@selector(tabbar:from:to:)]) {
        [self.delegate tabbar:self from:self.btn.tag to:btn.tag];
    }
    
}
// 设置空间的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    NSUInteger index = 0;
    for (UIView *obj in self.itemArray) {
        // 2.计算每个按钮的frame
        CGFloat objW = self.frame.size.width / 5;
        CGFloat objH = self.frame.size.height;
        CGFloat objX = index * objW;
        CGFloat objY = 0;
        if (index ==2) {
            index ++;
            objX = index * objW;
        }
        index ++;
    obj.frame = CGRectMake(objX, objY, objW,objH);
    }
    // 为加号按钮添加frame
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    
    
}


@end
